package com.knowledge.knowledge_notes.controller;

import com.knowledge.knowledge_notes.annotation.ApiRestController;
import com.knowledge.knowledge_notes.common.ResultVo;
import com.knowledge.knowledge_notes.pojo.JotterArticle;
import com.knowledge.knowledge_notes.service.JotterArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@ApiRestController
public class JotterController {

    @Autowired
    JotterArticleService jotterArticleService;

    @PostMapping("/admin/content/article")
    public ResultVo saveArticle(@RequestBody @Valid JotterArticle article) {
        jotterArticleService.addOrUpdate(article);
        return ResultVo.OK("保存成功");
    }

    @GetMapping("/article/{size}/{page}")
    public ResultVo listArticles(@PathVariable("size") int size, @PathVariable("page") int page) {
        return ResultVo.OK(jotterArticleService.list(page - 1, size));
    }

    @GetMapping("/article/{id}")
    public ResultVo getOneArticle(@PathVariable("id") int id) {
        return ResultVo.OK(jotterArticleService.findById(id));
    }

    @DeleteMapping("/admin/content/article")
    public ResultVo deleteArticle(@RequestParam("id") int id) {
        jotterArticleService.delete(id);
        return ResultVo.OK("删除成功");
    }

}
